/**
 * 
 */
package br.com.localfloripa.services;

import java.util.List;
import java.util.ResourceBundle;

import javax.inject.Inject;
import javax.inject.Named;
import javax.security.auth.login.LoginException;

import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import br.com.localfloripa.dao.AuditoriaDao;
import br.com.localfloripa.dto.AuditoriaDto;
import br.com.localfloripa.entities.TipoAuditoria;
import br.com.localfloripa.entities.Usuario;

/**
 * @author alan
 * @12/03/2012
 *
 */
@Service("estatisticaService")
@Scope("singleton")
public class EstatisticaService extends ServiceMain {

	private static final long serialVersionUID = 3992177016142116251L;
	
	
	@Inject
	@Named("auditoriaDao")
	private AuditoriaDao auditoriaDao;


	public EstatisticaService() {
		
	}
	
	/**
	 * Monta gráfico com estatísticas de acesso
	 * @return CartesianChartModel (Grafico)
	 * @throws Exception
	 */
	public CartesianChartModel barChartEstatisticasAcesso() throws Exception {
		
		List<AuditoriaDto> estatisticas = null;
		if (Usuario.hasRole("ADMIN")) {
			estatisticas = auditoriaDao.getAuditoriaGroupByTipo();
		} else if (Usuario.hasRole("CLIENTE") || Usuario.hasRole("IMOBILIARIA") || Usuario.hasRole("CORRETOR") ) {
			estatisticas = auditoriaDao.getAuditoriaByIdUsuarioGroupByTipo(Usuario.getLoggedUser().getId());
		} else {
			ResourceBundle bundle = ResourceBundle.getBundle("messages.MessageBundle");
			throw new LoginException(bundle.getString("messages.authority.error"));
		}			
		
		CartesianChartModel chartModel = new CartesianChartModel();
		
		ChartSeries album = new ChartSeries(bundle.getString("auditoria.chart.label.album"));
		ChartSeries telefone = new ChartSeries(bundle.getString("auditoria.chart.label.telefone"));
		ChartSeries site = new ChartSeries(bundle.getString("auditoria.chart.label.site"));
		for (AuditoriaDto auditoria : estatisticas) {
			
			if ( auditoria.getTipo().equals(TipoAuditoria.F.value())){
																
				album.set(auditoria.getAno().toString(), auditoria.getAcessos());				
			} else if ( auditoria.getTipo().equals(TipoAuditoria.S.value())) {
				
				site.set(auditoria.getAno().toString(), auditoria.getAcessos());				
			} else if ( auditoria.getTipo().equals(TipoAuditoria.T.value()) ) {
				
				telefone.set(auditoria.getAno().toString(), auditoria.getAcessos());
			}
			
		}
		chartModel.addSeries(album);
		chartModel.addSeries(telefone);
		chartModel.addSeries(site);
		
		return chartModel;		
	}
	

}
